namespace BM.Tools.WebControls.MForm.Controls.Helper
{    
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using BM.Tools.WebControls.MForm.Controls.Items;

    /// <summary>
    /// A control that displays all the buttons associated with an <see cref="OffspringItem" />.
    /// </summary>
    public class ButtonContainer : WebControl
    {
        private const string ButtonContainerSuffix = "BC";

        private ButtonContainerTitle title;        

        /// <summary>
        /// Initializes a new instance of the <see cref="ButtonContainer"/> class. 
        /// </summary>
        /// <param name="owner">
        /// The owner.
        /// </param>
        public ButtonContainer(OffspringItem owner)
        {
            Owner = owner;
            Buttons = new ControlCollection<Button>(Controls);
        }

        /// <summary>
        /// Gets buttons that are in the container.
        /// </summary>
        public Collection<Button> Buttons
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the owner of the button container.
        /// </summary>        
        public OffspringItem Owner
        {
            get; 
            private set;
        }

        /// <summary>
        /// Gets or sets the title of the button container displayed above the buttons.
        /// </summary>        
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public virtual ButtonContainerTitle Title
        {
            get
            {
                if (title == null)
                {
                    title = new ButtonContainerTitle { Text = Owner.DisplayName };
                }

                return title;
            }

            set
            {
                title = value;
            }
        }

        /// <summary>
        /// Gets the tag key - <see cref="HtmlTextWriterTag.Div" />.
        /// </summary>        
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        /// <summary>
        /// Sets an appropriate css class for the container
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, ButtonContainerSuffix);
        }

        /// <summary>
        /// Creates the button container title and the buttons
        /// </summary>
        protected override void CreateChildControls()
        {
            Controls.AddAt(0, Title);
            AddAssociatedButtons();
        }

        /// <summary>
        /// Checks if the button container should be displayed at all, because if it does not contain any child buttons, it should not be visible.
        /// </summary>
        /// <param name="e">
        /// The event args.
        /// </param>
        protected override void OnPreRender(System.EventArgs e)
        {
            base.OnPreRender(e);            
            if (Buttons.Count == 0)
            {
                Visible = false;
            }
        }

        private void AddAssociatedButtons()
        {
            var offspringsOwnerItem = Owner as OffspringsOwnerItem;
            if (offspringsOwnerItem != null)
            {
                offspringsOwnerItem.Offsprings.Select(offspring => offspring.AddButton).Where(button => button.Visible).ToList().ForEach(button => Buttons.Add(button));
            }
        }
    }
}